/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.cells.editors;

import cn.ac.nercis.pes.community.support.controls.cells.editors.base.EditorNodeBuilder;
import javafx.application.Platform;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Region;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 下拉列表单元格编辑器
 * @author zpy
 * @version 1.0.0
 */
public class ComboBoxEditorBuilder implements EditorNodeBuilder<String> {

    protected ComboBox<Label> comboBox;
    protected ObservableList<Label> items = new SimpleListProperty<>(FXCollections.observableArrayList());

    public ComboBoxEditorBuilder(List<String> items){
        this.items.addAll(items.stream().map(Label::new).collect(Collectors.toList()));
    }

    public ComboBoxEditorBuilder(ObservableList<Label> items){
        this.items = items;
    }

    @Override
    public void startEdit() {
        if(Objects.nonNull(comboBox)){
            Platform.runLater(()->comboBox.requestFocus());
        }
    }

    @Override
    public void cancelEdit() {

    }

    @Override
    public void updateItem(String s, boolean b) {
        if(Objects.nonNull(comboBox)){
            Platform.runLater(()->comboBox.requestFocus());
        }
    }

    @Override
    public Region createNode(String value, EventHandler<KeyEvent> eventHandler, ChangeListener<Boolean> changeListener) {
        comboBox = new ComboBox<>(items);
        comboBox.getStyleClass().add("table-editor-combobox");
        items.stream().filter(s-> StringUtils.equals(s.getText(),value))
                .findFirst()
                .ifPresent(i->comboBox.getSelectionModel().select(i));
        comboBox.setOnKeyPressed(eventHandler);
        comboBox.focusedProperty().addListener(changeListener);
        return comboBox;
    }

    @Override
    public void setValue(String value) {
        items.stream().filter(s-> StringUtils.equals(s.getText(),value))
                .findFirst()
                .ifPresent(i->comboBox.getSelectionModel().select(i));
    }

    @Override
    public String getValue() {
        return Objects.isNull(comboBox.getValue()) ? "" : comboBox.getValue().getText();
    }

    @Override
    public void validateValue() throws Exception {

    }

    @Override
    public void nullEditorNode() {
        comboBox = null;
    }
}